package com.huazi.www.dept.dao.impl;
/*
   User: 王有为
    Date: 2021/5/12
    Time: 16：56
 */

import com.huazi.www.dept.dao.DepartmentDao;
import com.huazi.www.dept.entity.Department;
import com.huazi.www.dept.entity.Department_CT;
import com.huazi.www.dept.util.BaoDao;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DepartmentDaoImpl extends BaoDao implements DepartmentDao {
    ResultSet rs = null;

    /**
     * 根据诊断id和患者名字
     *
     * @param userName 患者名字
     * @param statr    起始下标
     * @param pageSize 显示的行数
     * @return list
     */
    @Override
    public List<Department> list(String userName, Integer statr, Integer pageSize) {
        List<Department> list = new ArrayList<>();
        String sql = "SELECT dp.`userId`,fc.`name`,dp.`userTime`,dp.`userType`, dp.`state` FROM `department_queue` as dp,`frontdesk_card` as fc WHERE fc.id=dp.`userId` ";
        if (userName != null && !userName.equals("")) {
            sql += "  and  fc.`name` like CONCAT('%',?,'%') and dp.`state`=1 LIMIT ?,?";
            rs = executeQuery(sql, userName, statr, pageSize);
        } else {
            sql += " and dp.`state`=1 LIMIT ?,?";
            rs = executeQuery(sql, statr, pageSize);
        }
        try {
            while (rs.next()) {
                Integer id = rs.getInt(1);
                String userNames = rs.getString(2);
                String userTime = rs.getString(3);
                String userType = rs.getString(4);
                Integer state = rs.getInt(5);
                //调用实体类构造方法
                Department department = new Department(id, userNames, userTime, userType, state);
                list.add(department);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 新增CT记录
     *
     * @param ct
     * @return
     */
    @Override
    public int insertCt(Department_CT ct) {
        String sql = "INSERT INTO `department_ct`(CdiagnoseNO,CuserName,CTime,inspectionItem,diagnose,doctorid) VALUES(?,?,?,?,?,?);";
        String sqlUp = "update department_queue set state=3 where userId=?";
        int count = excyteUpdate(sql, ct.getCdiagnoseNo(), ct.getCuserName(), ct.getCTime(), ct.getInspectionItem(), ct.getDiagnose(), ct.getDoctorid());
        int countUp = excyteUpdate(sqlUp, ct.getCdiagnoseNo());
        if (count == 1 && countUp == 1) {
            return 1;
        }
        return 0;
    }


    /**
     * 查询总记录数
     *
     * @param userName 患者名字
     * @return
     */
    @Override
    public Integer count(String userName) {
        String sql = "SELECT count(1) \n" +
                "FROM `department_queue` as dp,`frontdesk_card` as fc WHERE fc.id=dp.`userId` ";
        if (userName != null && !userName.equals("")) {
            sql += " and  fc.`name` like CONCAT('%',?,'%') and dp.`state`=1 ";
            rs = executeQuery(sql, userName);
        } else {
            sql += " and dp.`state`=1";
            rs = executeQuery(sql);
        }
        int count = 0;
        try {
            while (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 查询id在修改
     *
     * @param id
     * @return
     */
    @Override
    public Department departmer(Integer id) {
        String sql = "SELECT fc.`name`,dp.`userTime`,dp.`userType`, dp.`state`FROM `department_queue` as dp,`frontdesk_card` as fc WHERE fc.id=dp.`userId` and dp.`id`=?";
        rs = executeQuery(sql, id);
        Department department = null;
        try {
            while (rs.next()) {
                String userNames = rs.getString(1);
                String userTime = rs.getString(2);
                String userType = rs.getString(3);
                Integer state = rs.getInt(4);
                //调用实体类构造方法
                department = new Department(userNames, userTime, userType, state);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return department;
    }

    /**
     * 根据诊断id修改排队号
     *
     * @param userid 诊断id
     * @param state  排队号
     * @return
     */
    @Override
    public Integer modify(Integer userid, Integer state) {
        String sql = "UPDATE department_queue set state =? where userId = ?";
        return excyteUpdate(sql, state, userid);
    }

    /**
     * 根据名字查询拍完照的记录
     *
     * @param name     名字
     * @param statr    下标
     * @param pageSize 显示行数
     * @return lis
     */
    @Override
    public List<Department> lis(String name, Integer statr, Integer pageSize) {
        List<Department> list = new ArrayList<>();
        String sql = "SELECT dp.`userId`,fc.`name`,dp.`userTime`,dp.`userType`, dp.`state` FROM `department_queue` as dp,`frontdesk_card` as fc WHERE fc.id=dp.`userId` ";
        if (name != null && !name.equals("")) {
            sql += "  and  fc.`name` like CONCAT('%',?,'%') and dp.`state`=2 LIMIT ?,?";
            rs = executeQuery(sql, name, statr, pageSize);
        } else {
            sql += " and dp.`state`=2 LIMIT ?,?";
            rs = executeQuery(sql, statr, pageSize);
        }
        try {
            while (rs.next()) {
                Integer id = rs.getInt(1);
                String userNames = rs.getString(2);
                String userTime = rs.getString(3);
                String userType = rs.getString(4);
                Integer state = rs.getInt(5);
                //调用实体类构造方法
                Department department = new Department(id, userNames, userTime, userType, state);
                list.add(department);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 查询拍完照的总记录数
     *
     * @param name 名字
     * @return
     */
    @Override
    public Integer countr(String name) {
        String sql = "SELECT count(1) \n" +
                "FROM `department_queue` as dp,`frontdesk_card` as fc WHERE fc.id=dp.`userId` ";
        if (name != null && !name.equals("")) {
            sql += " and  fc.`name` like CONCAT('%',?,'%') and dp.`state`=2 ";
            rs = executeQuery(sql, name);
        } else {
            sql += " and dp.`state`=2";
            rs = executeQuery(sql);
        }
        int countr = 0;
        try {
            while (rs.next()) {
                countr = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return countr;
    }
}




